<template>
  <el-dialog :title="!dataForm.id ? '新增' : '修改'" :close-on-click-modal="false" :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
      <el-form-item label="商品分类" prop="parentName">
        <el-popover
          ref="dataListPopover"
          placement="bottom-start"
          trigger="click">
          <el-tree
            :data="dataList"
            :props="dataListTreeProps"
            node-key="id"
            ref="dataListTree"
            @current-change="dataListTreeCurrentChangeHandle"
            :default-expand-all="true"
            :highlight-current="true"
            :expand-on-click-node="false">
          </el-tree>
        </el-popover>
        <el-input v-model="dataForm.parentName" v-popover:dataListPopover :readonly="true" placeholder="点击选择上级菜单" class="menu-list__input"></el-input>
      </el-form-item>
      <el-form-item label="产品名称" prop="productName">
        <el-input v-model="dataForm.productName" placeholder="产品名称"></el-input>
      </el-form-item>
      <el-form-item label="进价" prop="inPrice">
        <el-input v-model="dataForm.inPrice" placeholder="进价"></el-input>
      </el-form-item>
      <el-form-item label="售价" prop="price">
        <el-input v-model="dataForm.price" placeholder="售价"></el-input>
      </el-form-item>
      <!-- <el-form-item label="数量" prop="num">
        <el-input v-model="dataForm.num" placeholder="数量"></el-input>
      </el-form-item> -->
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  import { treeDataTranslate } from '@/utils'
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          categoryId: '',
          productName: '',
          inPrice: '',
          price: '',
          num: 0,
          creator: '',
          createTime: '',
          updator: '',
          updateTime: '',
          pid: '',
          pid2: '',
          fatherCategory: [],
          category: [],
          parentId: 0,
          parentName: ''
        },
        dataRule: {
          pid2: [{
            required: true,
            message: '二级分类不能为空',
            trigger: 'blur'
          }],
          productName: [{
            required: true,
            message: '产品名称不能为空',
            trigger: 'blur'
          }],
          num: [{
            required: true,
            message: '数量不能为空',
            trigger: 'blur'
          }]
        },
        dataList: [],
        dataListTreeProps: {
          label: 'category',
          children: 'children'
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/business/ckproduct/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({
              data
            }) => {
              if (data && data.code === 0) {
                console.log(data)
                this.dataForm.parentId = data.ckProduct.categoryId
                this.dataForm.parentName = data.ckProduct.categoryName
                this.dataForm.productName = data.ckProduct.productName
                this.dataForm.inPrice = data.ckProduct.inPrice
                this.dataForm.price = data.ckProduct.price
                this.dataForm.num = data.ckProduct.num
                this.dataForm.creator = data.ckProduct.creator
                this.dataForm.createTime = data.ckProduct.createTime
                this.dataForm.updator = data.ckProduct.updator
                this.dataForm.updateTime = data.ckProduct.updateTime
                this.getFatherCategory()
              }
            })
          }
        })
      },
      // 查询一级分类
      getFatherCategory () {
        this.$http({
          url: this.$http.adornUrl('/business/ckcategory/list'),
          method: 'get',
          params: this.$http.adornParams({

          })
        }).then(({
          data
        }) => {
          this.dataList = treeDataTranslate(data, 'id', 'pid')
        })
      },

      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/business/ckproduct/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'categoryId': this.dataForm.parentId,
                'productName': this.dataForm.productName,
                'inPrice': this.dataForm.inPrice,
                'price': this.dataForm.price,
                'creator': this.dataForm.creator,
                'createTime': this.dataForm.createTime,
                'updator': this.dataForm.updator,
                'updateTime': this.dataForm.updateTime
              })
            }).then(({
              data
            }) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      },
      // 菜单树选中
      dataListTreeCurrentChangeHandle (data, node) {
        this.dataForm.parentId = data.id
        this.dataForm.parentName = data.category
        this.pClose()
      },
      pClose () {
        this.$refs[`dataListPopover`].doClose()
      }
    }
  }
</script>
